Data Types in JavaScript

Data types define the type of value a variable can hold.

Categories of Data Types:

Data types can be categorized into two groups:

  1. Primitive Data Types - basic, immutable values
  2. Non-Primitive Data Types - objects, arrays, and functions

1. Primitive Data Types:-

Primitive Data Type are simple/basic and cannot be change once assigned (Immutable).

Types of Primitive Data Type in JavaScript:

There are 7 types of Primitive Data Types in JavaScript, They are: -

  1. Number
  2. Null
  3. String
  4. Symbol
  5. Boolean
  6. BigInt
  7. Undefined

1. Number:-

The Number type represent both integer and floating-point numbers.


            let a = 10;
            let b = 3.14;
            console.log(a);
            console.log(b);
        

Output:
10
3.14

2. String:-

String is a sequence of characters enclosed with single quotes(' '), double quotes(" "), or backticks(` `).


            let Fname = 'Shivam'; // Single Quotes
            console.log(Fname);
            
            let Lname = "Maurya"; // Double Quotes
            console.log(Lname );
            
            let Fullname = `${Fname} ${Lname}`; // backticks/Template literal
            console.log(Fullname );
        

Output:
Shivam
Maurya
Shivam Maurya

3. Boolean:-

Boolean Represents one of two values — either true or false.


            let isOnline = true;
            console.log(isOnline);

            let isOffline = false;
            console.log(isOffline);
        

Output:
true
false

4. Undefined:-

A variable that is declared but not assigned a value has type undefined.


            let x;
            console.log(x);
        

Output:
undefined

5. Null:-

A null Value represents an empty or Unknown value.


            let data = null;
            console.log(data);
        

Output:
null

6. Symbol:-

A Symbol is a unique and immutable primitive value that is mainly used to create unique property keys.

Symbols are always unique. It is Introduce in ES6 Update.


            let id = Symbol("id");
            console.log(id);
        

Output:
Symbol(id)

7. BigInt:-

BigInt is used to store very large integers beyond the safe limit of the Number type.

It is useful for Handling large numbers in cryptography, Precise calculations, ets.


            let bigNumber = 1234567890123456789012345678901234567890n;
            console.log(bigNumber);
        

Output:
1234567890123456789012345678901234567890n

2. Non-Primitive (Reference) Data Types:-

Non-primitive data types is a type of Data Type in JavaScript.

It is used to store the reference (Address of memory) instead of actual value.

Non-Primitive Data type can hold more than one value and can be change after creation. It is stored in heap memory and accessed by reference.

Types of Non-Primitive Data Type in JavaScript:

There are 3 types of Non-Primitive Data Types in JavaScript, They are: -

  1. Object
  2. Array
  3. Function

1. Object:-

An Object is a Non-Primitive data type.

Object is like a container or box that holds Information or Data in key value pair.

Where key is like a label (a property name) and value is data assigned to that key.

Key are always string (It can be or can not be written in Quotes) and Value can be any data type (String, number, boolean, array, function, or even another object).


            let person = {
            name: "Shivam",
            age: 21,
            Dob: "2002-10-02"
            };
            console.log(person);
        

Output:
{
name: 'Shivam',
age: 21,
Dob: '2002-10-02'
}

2. Array

An Array is a special type of variable or Object that can hold multiple values in a single place. Instead of creating separate variable for each values.

We can use an Array to store them in an ordered list.


            let fruits = ["Apple", "Banana", "Orange"];
            console.log(fruits);
        

Output:
[ 'Apple', 'Banana', 'Orange' ]

3. Function:-

A function is a reusable block of code that performs a specific tasks.

Or A function is a set of code that can be executed multiple time.

Instead of writing the same code again and again, we can write a function once and use it whenever we needed.


            function greet(name) {
                return "Hello, " + name + "!";
            }
            console.log(greet("Shivam"));
        

Output:
Hello, Shivam!

🔍Type Checking for Function and Array in JavaScript

1. Checking the Type of a Function

To check if a variable is a function, you can use the typeof operator.

Functions are objects, but they're given special handling by JavaScript, so the typeof operator returns "function" for them instead of "object".


            let myFunction = function() {
                return "Hello, World!";
            };
            console.log(typeof myFunction);
        

Output:
function

2. Checking the Type of an Array

Even though arrays are objects in JavaScript, we can check if a variable is an array using Array.isArray().


            let myArray = [1, 2, 3];
            console.log(typeof myArray); 
            console.log(Array.isArray(myArray)); 
        

Output:
object
true